package com.dengtao.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dengtao.common.R;
import com.dengtao.pojo.entity.VirusPlantInfectionRelationships;
import com.dengtao.pojo.param.VirusPlantInfectionRelationshipsQueryParam;
import com.dengtao.service.IVirusPlantInfectionRelationshipsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * <p>
 * 病毒-植物感染关系 前端控制器
 * </p>
 *
 * @author JiamingZhao
 * @since 2025-02-23
 */
@RestController
@CrossOrigin
@RequiredArgsConstructor
@RequestMapping("/virus-plant-infection-relationships")
public class VirusPlantInfectionRelationshipsController {

    private final IVirusPlantInfectionRelationshipsService service;

    @PostMapping("/list")
    public R<IPage<VirusPlantInfectionRelationships>> list(@RequestBody VirusPlantInfectionRelationshipsQueryParam param) {
        LambdaQueryWrapper<VirusPlantInfectionRelationships> wrapper = Wrappers.lambdaQuery();
        String keyWords = param.getKeyWords();
        if (StrUtil.isNotBlank(keyWords)) {
            wrapper.and(e -> e.like(StrUtil.isNotBlank(keyWords), VirusPlantInfectionRelationships::getVirusFamily, keyWords)
                    .or().like(StrUtil.isNotBlank(keyWords), VirusPlantInfectionRelationships::getVirusGenus, keyWords)
                    .or().like(StrUtil.isNotBlank(keyWords), VirusPlantInfectionRelationships::getVirusName, keyWords)
                    .or().like(StrUtil.isNotBlank(keyWords), VirusPlantInfectionRelationships::getVirusTaxId, keyWords)
                    .or().like(StrUtil.isNotBlank(keyWords), VirusPlantInfectionRelationships::getHostTaxId, keyWords)
                    .or().like(StrUtil.isNotBlank(keyWords), VirusPlantInfectionRelationships::getHostName, keyWords));
        }
        String virusExistencePattern = param.getVirusExistencePattern();
        if (StrUtil.isNotBlank(virusExistencePattern)) {
            if (Objects.equals(virusExistencePattern, "Unknow")) {
                //查询没有病毒模式的数据
                wrapper.eq(VirusPlantInfectionRelationships::getVirusExistencePattern, "");
            } else {
                wrapper.eq(VirusPlantInfectionRelationships::getVirusExistencePattern, virusExistencePattern);

            }
        }

        Page<VirusPlantInfectionRelationships> page = new Page<>(param.getCurrent(), param.getSize());
        Page<VirusPlantInfectionRelationships> relationshipsPage = service.page(page, wrapper);
        return R.success(relationshipsPage);
    }


}
